import com.fz.Mapper.StudentMapper;
import org.apache.commons.dbcp2.BasicDataSourceFactory;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.junit.Before;
import org.junit.Test;

import javax.sql.DataSource;
import java.io.IOException;
import java.util.Properties;

/**
 * Created by xiao zeng on 2017/6/23.
 *
 * dbcp配置及使用加入依赖
 */
public class StudentDome {

    protected SqlSessionFactory ss;
    protected SqlSession se;

    @Before
    public void init() throws Exception {
        Properties p = new Properties();
        p.setProperty("driverClassName", "com.mysql.jdbc.Driver");
        p.setProperty("url", "jdbc:mysql://localhost:3306/test");
        p.setProperty("username", "root");
        p.setProperty("password","root");
        p.setProperty("maxIdle", "10");
        p.setProperty("maxActive", "20");
        p.setProperty("maxWait", "-1");


        new BasicDataSourceFactory();
        DataSource ds = BasicDataSourceFactory.createDataSource(p);
        Environment env = new Environment("mysql", new JdbcTransactionFactory(), ds);
        this.ss  = new SqlSessionFactoryBuilder().build(new Configuration(env));
        // sqlSessionFactory.getConfiguration().addMapper(BookDao.class);//添加一个接口
        this.ss.getConfiguration().addMappers("com.fz.Mapper");// 添加所有接口
        this.se = this.ss.openSession();
    }

    @Test
    public void show(){
        StudentMapper sdao = se.getMapper(StudentMapper.class);
        sdao.query();
    }
}
